package com.eckey.lab.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @Author: ChengLiang
 * @CreateTime: 2023-04-27  11:00
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class RabbitMQListener {

    @RabbitListener(queues = "simple.queue")
    public void receiveMessage(String message) {
        System.out.println("接收到消息：" + message);
    }

    @RabbitListener(queues = "work.queue")
    public void receiveWorkMessage1(String message) throws InterruptedException {
        System.out.println("消息队列1...接收到消息：" + message);
        Thread.sleep(10);
    }

    @RabbitListener(queues = "work.queue")
    public void receiveWorkMessage2(String message) throws InterruptedException {
        System.out.println("消息队列2...接收到消息：" + message);
        Thread.sleep(15);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void receiveFanoutMessage1(String message) throws InterruptedException {
        System.out.println("消息队列1...接收到消息：" + message);
    }


    @RabbitListener(queues = "fanout.queue2")
    public void receiveFanoutMessage2(String message) throws InterruptedException {
        System.out.println("消息队列2...接收到消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("direct.queue1"),exchange = @Exchange(value = "direct.exchange",type = ExchangeTypes.DIRECT),key = {"red","blue"}))
    public void receiveDirectQueue1(String message) throws InterruptedException {
        System.out.println("消息队列1...接收到消息：" + message);
    }


    @RabbitListener(bindings = @QueueBinding(value = @Queue("direct.queue2"),exchange = @Exchange(value = "direct.exchange",type = ExchangeTypes.DIRECT),key = {"red","yellow"}))
    public void receiveDirectQueue2(String message) throws InterruptedException {
        System.out.println("消息队列2...接收到消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic.queue2"),exchange = @Exchange(value = "topic.exchange",type = ExchangeTypes.TOPIC),key = {"#.news"}))
    public void receiveTopicQueue2(String message) throws InterruptedException {
        System.out.println("消息队列2...接收到消息：" + message);
    }
}
